<?php

class Site_info extends MX_Controller {

	public function __construct()
	{
		parent::__construct();
		
		$this->load->library(array('form_validation', 'core'));
		
		$this->lang->load('site_info/site_info', 'indonesia');
		
		$this->load->helper('cookie');
		$this->load->helper('language');
		
		$this->load->model('site_info_model');
		$this->load->model('mmaster');
		
		$this->theme->set('page_title', lang('text_page_title'));
	}
	
	public function index()
	{
		if (!$this->session->userdata('is_logged_in')) {
			redirect('/login/' . $this->core->encode($this->uri->uri_string()));
		}
		
		$this->load->library('pagination');
		
		$per_page = 20;
		$offset = $this->uri->segment(4);
		$data['site_general'] = $this->site_info_model->get_all_site($per_page, $offset);
		
		$config = array(
			'base_url'		=> base_url('/referensi/wilayah/propinsi'),
			'total_rows'	=> $this->site_info_model->get_rows_site(),
			'per_page'		=> $per_page,
			'uri_segment'	=> 4
		);
		
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		if ($this->core->is_ajax()) {
			
			if ($this->input->get('cmd') == 'GET_MASTER_SITE') {
			
				$json = array();
				
				$master_site = $this->core->view_all('t_mtr_site_general', array('active' => 1), 'site_id');
				$data_site = array();
				foreach ($master_site as $mtr) {
					$mtr['active'] = $mtr['active'] == 1 ? 'Yes' : 'No';
					$data_site[] = $mtr;
				}
				$json = $data_site;
				
				echo json_encode($json);
				exit;
			}
		}
		
		$this->theme->set('page_title', 'PMTS - Site Info - Master Site');
		$this->theme->view('site_general_view', $data);
	}
	
	public function add()
	{
		if (!$this->session->userdata('is_logged_in')) {
			redirect('/login/' . $this->core->encode($this->uri->uri_string()));
		}
		
		if ($this->core->is_ajax()) {
		
			if ($this->input->get('cmd') == 'GET_KABUPATEN') {
			
				$propinsi_id = (int) $this->input->get('propinsi_id');
				
				$json = array();
				
				$kabupaten = $this->site_info_model->get_kabupaten($propinsi_id);
				foreach ($kabupaten as $kab) {
					$json[] = array('id' => $kab['kabupaten_id'], 'name' => $kab['descr']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->get('cmd') == 'GET_KECAMATAN') {
			
				$kabupaten_id = (int) $this->input->get('kabupaten_id');
				
				$json = array();
				
				$kecamatan = $this->site_info_model->get_kecamatan($kabupaten_id);
				foreach ($kecamatan as $kec) {
					$json[] = array('id' => $kec['kecamatan_id'], 'name' => $kec['descr']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->get('cmd') == 'GET_CME_PARTNER') {
			
				$json = array();
				
				$key = $this->input->get('key');
				
				$cme_partner = $this->site_info_model->get_cme_partner($key);
				if ($cme_partner) {
					$json['data'] = array();
					foreach ($cme_partner as $partner) {
						$json['data'][] = array(
							'id'	=> $partner['cme_partner_id'],
							'value'	=> trim($partner['nama']),
						);
					}
				} else {
					$json['data'][] = array('id' => '', 'value' => 'Results not found');
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->get('cmd') == 'GET_TENANT') {
			
				$json = array();
				
				$key = $this->input->get('key');
				
				$tenants = $this->site_info_model->get_tenant($key);
				if ($tenants) {
					$json['data'] = array();
					foreach ($tenants as $tenant) {
						$json['data'][] = array(
							'id'	=> $tenant['tenant_id'],
							'value'	=> trim($tenant['tenant_name']),
						);
					}
				} else {
					$json['data'][] = array('id' => '', 'value' => 'Results not found');
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'ADD_GENERAL_INFO') {
				
				$siteIdText = $this->input->post('siteIdText');
				$oldSiteId = $this->input->post('oldSiteId'); // reference id
				$siteName = $this->input->post('siteName');
				$siteType = $this->input->post('siteType');
				$siteProfile = $this->input->post('siteProfile');
				$siteLocationLat = $this->input->post('siteLocationLat') != '' ? $this->input->post('siteLocationLat') : NULL;
				$siteLocationLng = $this->input->post('siteLocationLng') != '' ? $this->input->post('siteLocationLng') : NULL;
				$siteAddress = $this->input->post('siteAddress');
				$propinsi = $this->input->post('propinsi');
				$kelurahan = $this->input->post('kelurahan');
				$kecamatan = $this->input->post('kecamatan');
				$kabupaten = $this->input->post('kabupaten');
				$kodePos = $this->input->post('kodePos');
				$siteSpecification = $this->input->post('siteSpecification');
				
				$costCenter = $this->input->post('costCenter');
				$branch = $this->input->post('branch');
				$subBranch = $this->input->post('subBranch');
				$cluster = $this->input->post('cluster');
				
				$guardHouse = $this->input->post('guardHouse') ? $this->input->post('guardHouse') : NULL;
				$_24HourAccess = $this->input->post('24HourAccess') ? $this->input->post('24HourAccess') : NULL;
				$cmeInsured = $this->input->post('cmeInsured') ? $this->input->post('cmeInsured') : NULL;
				$equipmentInsured = $this->input->post('equipmentInsured') ? $this->input->post('equipmentInsured') : NULL;
				
				$polisNumber = $this->input->post('polisNumber');
				$cmePartner = $this->input->post('cmePartner');
				$picSmart = $this->input->post('picSmart');
				$phonePicSmart = $this->input->post('phonePicSmart');
				$picCmePartner = $this->input->post('picCmePartner');
				$phonePicCmePartner = $this->input->post('phonePicCmePartner');
				$cutOverDate = $this->input->post('cutOverDate') != '' ? $this->input->post('cutOverDate') : NULL;
				$verifiedOnAir = $this->input->post('verifiedOnAir');
				
				$error = array();
				$json = array();
				
				if ($siteIdText == '') {
					$error[] = 'Site ID can not empty.';
				}
				if ($this->core->view_all('t_mtr_site_general', array('site_id_text' => $siteIdText))) {
					$error[] = 'Site ID has already registered.';
				}
				if ($oldSiteId == '') {
					$error[] = 'Reference Site ID can not empty.';
				}
				if ($this->site_info_model->check_old_site_id($oldSiteId)) {
					$error[] = 'Reference Site ID has already registered.';
				}
				if ($siteName == '') {
					$error[] = 'Site Name can not empty.';
				}
				if ($siteType == '') {
					$error[] = 'Site Type can not empty.';
				}
				if ($siteProfile == '') {
					$error[] = 'Site Profile can not empty.';
				}
				
				if (count($error) == 0) {
					
					$this->core->set['old_site_id'] = $oldSiteId;
					$this->core->set['site_id_text'] = $siteIdText;
					$this->core->set['site_name'] = $siteName;
					$this->core->set['site_type'] = (int) $siteType;
					$this->core->set['site_profile'] = (int) $siteProfile;
					$this->core->set['latitude'] = $siteLocationLat;
					$this->core->set['longitude'] = $siteLocationLng;
					$this->core->set['site_address'] = $siteAddress;
					$this->core->set['kelurahan'] = $kelurahan;
					$this->core->set['kecamatan'] = (int) $kecamatan;
					$this->core->set['kabupaten'] = (int) $kabupaten;
					$this->core->set['propinsi'] = (int) $propinsi;
					$this->core->set['kode_pos'] = $kodePos;
					$this->core->set['site_specification'] = (int) $siteSpecification;
					$this->core->set['cost_center'] = $costCenter;
					$this->core->set['branch'] = $branch;
					$this->core->set['sub_branch'] = $subBranch;
					$this->core->set['cluster'] = $cluster;
					$this->core->set['guard_house'] = $guardHouse;
					$this->core->set['24_hour_access'] = $_24HourAccess;
					$this->core->set['cme_insured'] = $cmeInsured;
					$this->core->set['equipment_insured'] = $equipmentInsured;
					$this->core->set['polis_number'] = $polisNumber;
					$this->core->set['cme_partner'] = $cmePartner;
					$this->core->set['pic_smart'] = $picSmart;
					$this->core->set['phone1'] = $phonePicSmart;
					$this->core->set['pic_cme_partner'] = $picCmePartner;
					$this->core->set['phone2'] = $phonePicCmePartner;
					$this->core->set['cut_over_date'] = $cutOverDate;
					$this->core->set['verified_on_air'] = $verifiedOnAir;
					$this->core->set['created_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['created_by'] = $this->session->userdata('username');
					$this->core->set['status'] = 1;
					$this->core->set['active'] = 1;
					
					$this->core->save('t_mtr_site_general');
					$site_id = $this->core->last_insert_id;
					
					$json = array('error' => FALSE, 'message' => 'General Info added.', 'site_id' => $site_id);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => '');
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'ADD_SITE_KEEPERS') {
			
				$siteId = $this->input->post('siteId');
				$name = $this->input->post('name');
				$address = $this->input->post('address');
				$phone = $this->input->post('phone');
				$salary = $this->input->post('salary');
				$hiredBySmart = $this->input->post('hiredBySmart');
				$remark = $this->input->post('remark');
				
				$error = array();
				$json = array();
				
				if ($name == '') {
					$error[] = 'Name can not empty.';
				}
				if ($address == '') {
					$error[] = 'Address can not empty.';
				}
				if ($siteId == '') {
					$error = array('Data General Info is not complete.');
				}
				
				if (count($error) == 0) {
					
					$this->core->set['site_id'] = $siteId;
					$this->core->set['name'] = $name;
					$this->core->set['address'] = $address;
					$this->core->set['phone'] = $phone;
					$this->core->set['salary_month'] = $salary;
					$this->core->set['hired_by_smart'] = $hiredBySmart;
					$this->core->set['remark'] = $remark;
					$this->core->set['updated_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['updated_by'] = $this->session->userdata('username');
					$this->core->set['status'] = 1;
					
					if ($siteId != '') {
						if ( ! $this->core->view_all('t_mtr_site_l_keepers', array('site_id' => $siteId))) {
							$this->core->save('t_mtr_site_l_keepers');
						} else {
							$this->core->save('t_mtr_site_l_keepers', array('site_id' => $siteId));
						}
					}
					
					$json = array('error' => FALSE, 'message' => 'Site Keepers added.', 'site_id' => $siteId);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $siteId);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'ADD_PLN') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
					
					$this->core->set['site_id'] = $post['siteId'];
					$this->core->set['handover_project'] = $post['handoverProject'];
					$this->core->set['handover_remark'] = $post['handoverRemark'];
					$this->core->set['id_pelanggan_pln'] = $post['idPelangganPLN'];
					$this->core->set['atas_nama_pelanggan'] = $post['atasNamaPelanggan'];
					$this->core->set['pln_connected_date'] = $post['plnConnectedDate'] != '' ? $post['plnConnectedDate'] : NULL;
					$this->core->set['nama_upj_pln'] = $post['namaUPJPLN'];
					$this->core->set['wo_dismantle_date'] = $post['woDismantleDate'] != '' ? $post['woDismantleDate'] : NULL;
					$this->core->set['trafo_blast'] = (int) $post['trafoBlast'];
					$this->core->set['trafo_capacity'] = (int) $post['trafoCapacity'];
					$this->core->set['trafo_maintained_by_smartfren'] = (int) $post['trafoMaintainedBySmartfren'];
					$this->core->set['kwh_type'] = (int) $post['kwhType'];
					$this->core->set['pln_phase'] = $post['plnPhase'];
					$this->core->set['pln_capacity'] = $post['plnCapacity'];
					$this->core->set['tarif_type'] = (int) $post['tarifType'];
					$this->core->set['tarif_name'] = (int) $post['tarifName'];
					$this->core->set['cara_pembayaran'] = (int) $post['caraPembayaran'];
					$this->core->set['remarks'] = $post['remarks'];
					$this->core->set['permanent_genset'] = $post['permanentGenset'];
					$this->core->set['genset_brand'] = $post['gensetBrand'];
					$this->core->set['genset_capacity'] = $post['gensetCapacity'];
					$this->core->set['genset_ownership'] = (int) $post['gensetOwnership'];
					$this->core->set['created_by'] = $this->session->userdata('username');
					$this->core->set['created_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['active'] = 1;
					$this->core->set['status'] = 1;
					
					if ($post['siteId'] != '') {
						if ( ! $this->core->view_all('t_mtr_site_l_pln', array('site_id' => $post['siteId']))) {
							$this->core->save('t_mtr_site_l_pln');
						} else {
							$this->core->save('t_mtr_site_l_pln', array('site_id' => $post['siteId']));
						}
					}
					$json = array('error' => FALSE, 'message' => 'PLN added.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'ADD_TOWER') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
					
					$this->core->set['site_id'] = $post['siteId'];
					$this->core->set['kepemilikan_tower'] = (int) $post['kepemilikanTower'];
					$this->core->set['nama_pemilik_tower'] = $post['namaPemilikTower'];
					$this->core->set['no_telp_pemilik_tower'] = $post['telpPemilikTower'];
					$this->core->set['tower_type'] = (int) $post['towerType'];
					$this->core->set['tower_leg_no'] = (int) $post['towerLegNo'];
					$this->core->set['tower_height'] = $post['towerHeight'];
					$this->core->set['tower_fabricator'] = $post['towerFabricator'];
					$this->core->set['remarks'] = $post['remarks'];
					$this->core->set['updated_by'] = $this->session->userdata('username');
					$this->core->set['updated_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['active'] = 1;
					$this->core->set['status'] = 1;
					
					if ($post['siteId'] != '') {
						if ( ! $this->core->view_all('t_mtr_site_l_tower', array('site_id' => $post['siteId']))) {
							$this->core->save('t_mtr_site_l_tower');
						} else {
							$this->core->save('t_mtr_site_l_tower', array('site_id' => $post['siteId']));
						}
					}
					$json = array('error' => FALSE, 'message' => 'Tower added.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'ADD_SHELTER') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
					
					$this->core->set['site_id'] = $post['siteId'];
					$this->core->set['kepemilikan_shelter'] = $post['kepemilikanShelter'];
					$this->core->set['nama_pemilik_shelter'] = $post['namaPemilikShelter'];
					$this->core->set['no_telp_pemilik_shelter'] = $post['noTelpPemilikShelter'];
					$this->core->set['shelter_type'] = $post['shelterType'];
					$this->core->set['shelter_size'] = $post['shelterSize'];
					$this->core->set['shelter_fabricator'] = $post['shelterFabricator'];
					$this->core->set['remarks'] = $post['remarks'];
					$this->core->set['created_by'] = $this->session->userdata('username');
					$this->core->set['created_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['active'] = 1;
					$this->core->set['status'] = 1;
					
					if ($post['siteId'] != '') {
						if ( ! $this->core->view_all('t_mtr_site_l_shelter', array('site_id' => $post['siteId']))) {
							$this->core->save('t_mtr_site_l_shelter');
						} else {
							$this->core->save('t_mtr_site_l_shelter', array('site_id' => $post['siteId']));
						}
					}
					$json = array('error' => FALSE, 'message' => 'Shelter added.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'ADD_LAHAN') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
				
					$this->core->set['site_id'] = $post['siteId'];
					$this->core->set['kepemilikan_lahan'] = $post['kepemilikanLahan'];
					$this->core->set['jenis_surat_lahan'] = $post['jenisSuratLahan'];
					$this->core->set['no_ajb'] = $post['noAJB'];
					$this->core->set['date_ajb'] = $post['dateAJB'] != '' ? $post['dateAJB'] : NULL;
					$this->core->set['no_surat_lahan'] = $post['noSuratLahan'];
					$this->core->set['luas_lahan'] = $post['luasLahan'];
					$this->core->set['akses_jalan_pjg'] = $post['aksesJalanPanjang'];
					$this->core->set['akses_jalan_lbr'] = $post['aksesJalanLebar'];
					$this->core->set['sudah_balik_nama'] = $post['sudahBalikNama'];
					$this->core->set['no_baru'] = $post['noBaru'];
					$this->core->set['lease_term'] = $post['leaseTerm'];
					$this->core->set['lease_start_date'] = $post['leaseStartDate'] != '' ? $post['leaseStartDate'] : NULL;
					$this->core->set['lease_finish_date'] = $post['leaseFinishDate'] != '' ? $post['leaseFinishDate'] : NULL;
					$this->core->set['nama_pemilik_lahan'] = $post['namaPemilikLahan'];
					$this->core->set['contact_person_lahan'] = $post['noTelpPemilikLahan'];
					$this->core->set['no_telp_pemilik_lahan'] = $post['noTelpPemilikLahan'];
					$this->core->set['contact_person_lahan'] = $post['contactPersonLahan'];
					$this->core->set['no_telp_contact_person'] = $post['noTelpContactPerson'];
					$this->core->set['status'] = $post['status'];
					$this->core->set['company'] = $post['company'];
					$this->core->set['created_by'] = $this->session->userdata('username');
					$this->core->set['created_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['active'] = 1;
					
					if ($post['siteId'] != '') {
						if ( ! $this->core->view_all('t_mtr_site_l_lahan', array('site_id' => $post['siteId']))) {
							$this->core->save('t_mtr_site_l_lahan');
						} else {
							$this->core->save('t_mtr_site_l_lahan', array('site_id' => $post['siteId']));
						}
					}
					$json = array('error' => FALSE, 'message' => 'Lahan added.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'ADD_PERIJINAN') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
				
					$this->core->set['site_id'] = $post['siteId'];
					$this->core->set['iw_needed'] = $post['iwNeeded'];
					$this->core->set['iw_date'] = $post['iwDate'] != '' ? $post['iwDate'] : NULL;
					$this->core->set['retribusi'] = $post['retribusi'];
					$this->core->set['biaya_retribusi'] = $post['biayaRetribusi'];
					$this->core->set['ho_number'] = $post['hoNumber'];
					$this->core->set['ho_date'] = $post['hoDate'] != '' ? $post['hoDate'] : NULL;
					$this->core->set['ho_expired'] = $post['hoExpired'] != '' ? $post['hoExpired'] : NULL;
					$this->core->set['nomor_pbb'] = $post['nomorPBB'];
					$this->core->set['atas_nama'] = $post['atasNama'];
					$this->core->set['jatuh_tempo'] = $post['jatuhTempo'] != '' ? $post['jatuhTempo'] : NULL;
					$this->core->set['nilai_pbb'] = $post['nilaiPBB'];
					$this->core->set['pic_pbb'] = $post['picPBB'];
					$this->core->set['nomor_imb'] = $post['nomorIMB'];
					$this->core->set['date'] = $post['imbDate'] != '' ? $post['imbDate'] : NULL;
					$this->core->set['expired'] = $post['imbExpired'] != '' ? $post['imbExpired'] : NULL;
					$this->core->set['rkt_number'] = $post['rktNumber'];
					$this->core->set['rkt_date'] = $post['rktDate'] != '' ? $post['rktDate'] : NULL;
					$this->core->set['remarks'] = $post['remarks'];
					$this->core->set['created_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['created_by'] = $this->session->userdata('username');
					$this->core->set['active'] = 1;
					$this->core->set['status'] = 1;
					
					if ($post['siteId'] != '') {
						if ( ! $this->core->view_all('t_mtr_site_l_perijinan', array('site_id' => $post['siteId']))) {
							$this->core->save('t_mtr_site_l_perijinan');
						} else {
							$this->core->save('t_mtr_site_l_perijinan', array('site_id' => $post['siteId']));
						}
					}
					$json = array('error' => FALSE, 'message' => 'Perijinan added.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'ADD_TENANCY') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
					
					// save for colloc sell
					$this->db->delete('t_mtr_site_l_tenancy', array('site_id' => $post['siteId'], 'tenancy_type' => 'COLLOC_SELL'));
					
					$date = date('Y-m-d H:i:s.u');
					$order = 1;
					foreach ($post['tenantCollocSell'] as $kvsell => $vcsell) {
						if ($vcsell != '') {
							$this->core->set['site_id'] = $post['siteId'];
							$this->core->set['available_for_rent'] = $post['availableForRent'];
							$this->core->set['tenancy_type'] = 'COLLOC_SELL';
							$this->core->set['tenant_id'] = $vcsell;
							$this->core->set['tenancy_date'] = $post['tenantDateCollocSell'][$kvsell] != '' ? $post['tenantDateCollocSell'][$kvsell] : NULL;
							$this->core->set['order'] = $order;
							$this->core->set['created_by'] = $this->session->userdata('username');
							$this->core->set['created_on'] = $date;
							$this->core->set['updated_by'] = $this->session->userdata('username');
							$this->core->set['updated_on'] = $date;
							$this->core->set['active'] = 1;
							
							$this->core->save('t_mtr_site_l_tenancy');
						}
						$order++;
					}
					
					// save for colloc buy
					if ($post['seqNumberOfTenant'] > 0) {
						$this->db->delete('t_mtr_site_l_tenancy', array('site_id' => $post['siteId'], 'tenancy_type' => 'COLLOC_BUY'));
						
						$date = date('Y-m-d H:i:s.u');
						$order = 1;
						foreach ($post['tenantCollocBuy'] as $kvbuy => $vcbuy) {
							if ($vcsell != '') {
								$this->core->set['site_id'] = $post['siteId'];
								$this->core->set['seq_number'] = (int) $post['seqNumberOfTenant'];
								$this->core->set['tenancy_type'] = 'COLLOC_BUY';
								$this->core->set['tenant_id'] = (int) $vcbuy;
								$this->core->set['tenancy_date'] = $post['tenantDateCollocBuy'][$kvbuy] != '' ? $post['tenantDateCollocBuy'][$kvbuy] : NULL;
								$this->core->set['order'] = $order;
								$this->core->set['created_by'] = $this->session->userdata('username');
								$this->core->set['created_on'] = $date;
								$this->core->set['updated_by'] = $this->session->userdata('username');
								$this->core->set['updated_on'] = $date;
								$this->core->set['active'] = 1;
								
								$this->core->save('t_mtr_site_l_tenancy');
							}
							$order++;
						}
					}
					
					$json = array('error' => FALSE, 'message' => 'Tenancy added.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'ADD_AC') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['brandAC1'] == '') {
					$error[] = 'AC1 Brand can not empty.';
				}
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
				
					$this->core->set['site_id'] = (int) $post['siteId'];
					$this->core->set['ac1_brand'] = (int) $post['brandAC1'];
					$this->core->set['ac1_type'] = $post['typeAC1'];
					$this->core->set['ac1_capacity'] = (int) $post['capacityAC1'];
					$this->core->set['ac1_serial'] = $post['serialNumberAC1'];
					$this->core->set['ac2_brand'] = (int) $post['brandAC2'];
					$this->core->set['ac2_type'] = $post['typeAC2'];
					$this->core->set['ac2_capacity'] = (int) $post['capacityAC2'];
					$this->core->set['ac2_serial'] = $post['serialNumberAC2'];
					$this->core->set['ac3_brand'] = (int) $post['brandAC3'];
					$this->core->set['ac3_type'] = $post['typeAC3'];
					$this->core->set['ac3_capacity'] = (int) $post['capacityAC3'];
					$this->core->set['ac3_serial'] = $post['serialNumberAC3'];
					$this->core->set['remarks'] = trim($post['remarks']);
					
					if ($post['siteId'] != '') {
						if ( ! $this->core->view_all('t_mtr_site_l_ac', array('site_id' => $post['siteId']))) {
							$this->core->save('t_mtr_site_l_ac');
						} else {
							$this->core->save('t_mtr_site_l_ac', array('site_id' => $post['siteId']));
						}
					}
					$json = array('error' => FALSE, 'message' => 'AC added.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
		}
		
		$this->_site_form();
	}
	
	public function edit($id = '')
	{
		if (!$this->session->userdata('is_logged_in')) {
			redirect('/login/' . $this->core->encode($this->uri->uri_string()));
		}
		
		if (empty($id)) {
			redirect('/site_info');
		}
		
		if ($this->core->is_ajax()) {
		
			if ($this->input->post('cmd') == 'EDIT_GENERAL_INFO') {
				
				$siteId = $this->input->post('siteId');
				$oldSiteId = $this->input->post('oldSiteId');
				$siteName = $this->input->post('siteName');
				$siteType = $this->input->post('siteType');
				$siteProfile = $this->input->post('siteProfile');
				$siteLocationLat = $this->input->post('siteLocationLat');
				$siteLocationLng = $this->input->post('siteLocationLng');
				$siteAddress = $this->input->post('siteAddress');
				$propinsi = $this->input->post('propinsi');
				$kelurahan = $this->input->post('kelurahan');
				$kecamatan = $this->input->post('kecamatan');
				$kabupaten = $this->input->post('kabupaten');
				$kodePos = $this->input->post('kodePos');
				$siteSpecification = $this->input->post('siteSpecification');
				
				$costCenter = $this->input->post('costCenter');
				$branch = $this->input->post('branch');
				$subBranch = $this->input->post('subBranch');
				$cluster = $this->input->post('cluster');
				
				$guardHouse = $this->input->post('guardHouse') ? $this->input->post('guardHouse') : NULL;
				$_24HourAccess = $this->input->post('24HourAccess') ? $this->input->post('24HourAccess') : NULL;
				$cmeInsured = $this->input->post('cmeInsured') ? $this->input->post('cmeInsured') : NULL;
				$equipmentInsured = $this->input->post('equipmentInsured') ? $this->input->post('equipmentInsured') : NULL;
				
				$polisNumber = $this->input->post('polisNumber');
				$cmePartner = $this->input->post('cmePartner');
				$picSmart = $this->input->post('picSmart');
				$phonePicSmart = $this->input->post('phonePicSmart');
				$picCmePartner = $this->input->post('picCmePartner');
				$phonePicCmePartner = $this->input->post('phonePicCmePartner');
				$cutOverDate = $this->input->post('cutOverDate') != '' ? $this->input->post('cutOverDate') : NULL;
				$verifiedOnAir = $this->input->post('verifiedOnAir');
				
				$error = array();
				$json = array();
				
				if ($oldSiteId == '') {
					$error[] = 'Reference Site ID can not empty.';
				}
				if ($siteName == '') {
					$error[] = 'Site Name can not empty.';
				}
				
				if (count($error) == 0) {
					
					$this->core->set['old_site_id'] = $oldSiteId;
					//$this->core->set['site_id_text'] = $siteIdText;
					$this->core->set['site_name'] = $siteName;
					$this->core->set['site_type'] = (int) $siteType;
					$this->core->set['site_profile'] = (int) $siteProfile;
					$this->core->set['latitude'] = $siteLocationLat;
					$this->core->set['longitude'] = $siteLocationLng;
					$this->core->set['site_address'] = $siteAddress;
					$this->core->set['kelurahan'] = $kelurahan;
					$this->core->set['kecamatan'] = (int) $kecamatan;
					$this->core->set['kabupaten'] = (int) $kabupaten;
					$this->core->set['propinsi'] = (int) $propinsi;
					$this->core->set['kode_pos'] = $kodePos;
					$this->core->set['site_specification'] = (int) $siteSpecification;
					$this->core->set['cost_center'] = $costCenter;
					$this->core->set['branch'] = $branch;
					$this->core->set['sub_branch'] = $subBranch;
					$this->core->set['cluster'] = $cluster;
					$this->core->set['guard_house'] = $guardHouse;
					$this->core->set['24_hour_access'] = $_24HourAccess;
					$this->core->set['cme_insured'] = $cmeInsured;
					$this->core->set['equipment_insured'] = $equipmentInsured;
					$this->core->set['polis_number'] = $polisNumber;
					$this->core->set['cme_partner'] = $cmePartner;
					$this->core->set['pic_smart'] = $picSmart;
					$this->core->set['phone1'] = $phonePicSmart;
					$this->core->set['pic_cme_partner'] = $picCmePartner;
					$this->core->set['phone2'] = $phonePicCmePartner;
					$this->core->set['cut_over_date'] = $cutOverDate;
					$this->core->set['verified_on_air'] = $verifiedOnAir;
					$this->core->set['updated_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['updated_by'] = $this->session->userdata('username');
					$this->core->set['status'] = 1;
					$this->core->set['active'] = 1;
					
					$this->core->save('t_mtr_site_general', array('site_id' => $id));
					
					$json = array('error' => FALSE, 'message' => 'General Info edited.', 'site_id' => $siteId);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $siteId);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'EDIT_SITE_KEEPERS') {
				
				$siteId = $this->input->post('siteId');
				$name = $this->input->post('name');
				$address = $this->input->post('address');
				$phone = $this->input->post('phone');
				$salary = $this->input->post('salary');
				$hiredBySmart = $this->input->post('hiredBySmart');
				$remark = $this->input->post('remark');
				
				$error = array();
				$json = array();
				
				if ($name == '') {
					$error[] = 'Name can not empty.';
				}
				if ($address == '') {
					$error[] = 'Address can not empty.';
				}
				
				if (count($error) == 0) {
					
					$this->core->set['site_id'] = $siteId;
					$this->core->set['name'] = $name;
					$this->core->set['address'] = $address;
					$this->core->set['phone'] = $phone;
					$this->core->set['salary_month'] = $salary;
					$this->core->set['hired_by_smart'] = $hiredBySmart;
					$this->core->set['remark'] = $remark;
					$this->core->set['updated_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['updated_by'] = $this->session->userdata('username');
					$this->core->set['status'] = 1;
					
					if ($this->site_info_model->check_site_keepers($id)) {
						$this->core->save('t_mtr_site_l_keepers', array('site_id' => $id));
					} else {
						$this->core->save('t_mtr_site_l_keepers');
					}
					
					$json = array('error' => FALSE, 'message' => 'Data Site Keepers telah diupdate.', 'site_id' => $siteId);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $siteId);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'EDIT_PLN') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
					
					$this->core->set['site_id'] = $post['siteId'];
					$this->core->set['handover_project'] = $post['handoverProject'];
					$this->core->set['handover_remark'] = $post['handoverRemark'];
					$this->core->set['id_pelanggan_pln'] = $post['idPelangganPLN'];
					$this->core->set['atas_nama_pelanggan'] = $post['atasNamaPelanggan'];
					$this->core->set['pln_connected_date'] = $post['plnConnectedDate'] != '' ? $post['plnConnectedDate'] : NULL;
					$this->core->set['nama_upj_pln'] = $post['namaUPJPLN'];
					$this->core->set['wo_dismantle_date'] = $post['woDismantleDate'] != '' ? $post['woDismantleDate'] : NULL;
					$this->core->set['trafo_blast'] = (int) $post['trafoBlast'];
					$this->core->set['trafo_capacity'] = (int) $post['trafoCapacity'];
					$this->core->set['trafo_maintained_by_smartfren'] = (int) $post['trafoMaintainedBySmartfren'];
					$this->core->set['kwh_type'] = (int) $post['kwhType'];
					$this->core->set['pln_phase'] = $post['plnPhase'];
					$this->core->set['pln_capacity'] = $post['plnCapacity'];
					$this->core->set['tarif_type'] = (int) $post['tarifType'];
					$this->core->set['tarif_name'] = (int) $post['tarifName'];
					$this->core->set['cara_pembayaran'] = (int) $post['caraPembayaran'];
					$this->core->set['remarks'] = $post['remarks'];
					$this->core->set['permanent_genset'] = $post['permanentGenset'];
					$this->core->set['genset_brand'] = $post['gensetBrand'];
					$this->core->set['genset_capacity'] = $post['gensetCapacity'];
					$this->core->set['genset_ownership'] = (int) $post['gensetOwnership'];
					$this->core->set['updated_by'] = $this->session->userdata('username');
					$this->core->set['updated_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['active'] = 1;
					$this->core->set['status'] = 1;
					
					if ( ! $this->core->view_all('t_mtr_site_l_pln', array('site_id' => $id))) {
						$this->core->save('t_mtr_site_l_pln');
					} else {
						$this->core->save('t_mtr_site_l_pln', array('site_id' => $id));
					}
					$json = array('error' => FALSE, 'message' => 'PLN edited.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'EDIT_TOWER') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
					
					$this->core->set['site_id'] = $post['siteId'];
					$this->core->set['kepemilikan_tower'] = (int) $post['kepemilikanTower'];
					$this->core->set['nama_pemilik_tower'] = $post['namaPemilikTower'];
					$this->core->set['no_telp_pemilik_tower'] = $post['telpPemilikTower'];
					$this->core->set['tower_type'] = (int) $post['towerType'];
					$this->core->set['tower_leg_no'] = (int) $post['towerLegNo'];
					$this->core->set['tower_height'] = $post['towerHeight'];
					$this->core->set['tower_fabricator'] = $post['towerFabricator'];
					$this->core->set['remarks'] = $post['remarks'];
					$this->core->set['updated_by'] = $this->session->userdata('username');
					$this->core->set['updated_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['active'] = 1;
					$this->core->set['status'] = 1;
					
					if ( ! $this->core->view_all('t_mtr_site_l_tower', array('site_id' => $id))) {
						$this->core->save('t_mtr_site_l_tower');
					} else {
						$this->core->save('t_mtr_site_l_tower', array('site_id' => $id));
					}
					$json = array('error' => FALSE, 'message' => 'Tower edited.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'EDIT_SHELTER') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
				
					$this->core->set['site_id'] = $post['siteId'];
					$this->core->set['kepemilikan_shelter'] = $post['kepemilikanShelter'];
					$this->core->set['nama_pemilik_shelter'] = $post['namaPemilikShelter'];
					$this->core->set['no_telp_pemilik_shelter'] = $post['noTelpPemilikShelter'];
					$this->core->set['shelter_type'] = $post['shelterType'];
					$this->core->set['shelter_size'] = $post['shelterSize'];
					$this->core->set['shelter_fabricator'] = $post['shelterFabricator'];
					$this->core->set['remarks'] = $post['remarks'];
					$this->core->set['updated_by'] = $this->session->userdata('username');
					$this->core->set['updated_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['active'] = 1;
					$this->core->set['status'] = 1;
					
					if ( ! $this->core->view_all('t_mtr_site_l_shelter', array('site_id' => $id))) {
						$this->core->save('t_mtr_site_l_shelter');
					} else {
						$this->core->save('t_mtr_site_l_shelter', array('site_id' => $id));
					}					
					$json = array('error' => FALSE, 'message' => 'Shelter edited.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'EDIT_LAHAN') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
					
					$this->core->set['site_id'] = $post['siteId'];
					$this->core->set['kepemilikan_lahan'] = $post['kepemilikanLahan'];
					$this->core->set['jenis_surat_lahan'] = $post['jenisSuratLahan'];
					$this->core->set['no_ajb'] = $post['noAJB'];
					$this->core->set['date_ajb'] = $post['dateAJB'] != '' ? $post['dateAJB'] : NULL;
					$this->core->set['no_surat_lahan'] = $post['noSuratLahan'];
					$this->core->set['luas_lahan'] = $post['luasLahan'];
					$this->core->set['akses_jalan_pjg'] = $post['aksesJalanPanjang'];
					$this->core->set['akses_jalan_lbr'] = $post['aksesJalanLebar'];
					$this->core->set['sudah_balik_nama'] = $post['sudahBalikNama'];
					$this->core->set['no_baru'] = $post['noBaru'];
					$this->core->set['lease_term'] = $post['leaseTerm'];
					$this->core->set['lease_start_date'] = $post['leaseStartDate'] != '' ? $post['leaseStartDate'] : NULL;
					$this->core->set['lease_finish_date'] = $post['leaseFinishDate'] != '' ? $post['leaseFinishDate'] : NULL;
					$this->core->set['nama_pemilik_lahan'] = $post['namaPemilikLahan'];
					$this->core->set['contact_person_lahan'] = $post['noTelpPemilikLahan'];
					$this->core->set['no_telp_pemilik_lahan'] = $post['noTelpPemilikLahan'];
					$this->core->set['contact_person_lahan'] = $post['contactPersonLahan'];
					$this->core->set['no_telp_contact_person'] = $post['noTelpContactPerson'];
					$this->core->set['status'] = $post['status'];
					$this->core->set['company'] = $post['company'];
					$this->core->set['updated_by'] = $this->session->userdata('username');
					$this->core->set['updated_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['active'] = 1;
					
					if ( ! $this->core->view_all('t_mtr_site_l_lahan', array('site_id' => $id))) {
						$this->core->save('t_mtr_site_l_lahan');
					} else {
						$this->core->save('t_mtr_site_l_lahan', array('site_id' => $id));
					}					
					$json = array('error' => FALSE, 'message' => 'Lahan edited.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'EDIT_PERIJINAN') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
					
					$this->core->set['site_id'] = $post['siteId'];
					$this->core->set['iw_needed'] = $post['iwNeeded'];
					$this->core->set['iw_date'] = $post['iwDate'] != '' ? $post['iwDate'] : NULL;
					$this->core->set['retribusi'] = $post['retribusi'];
					$this->core->set['biaya_retribusi'] = $post['biayaRetribusi'];
					$this->core->set['ho_number'] = $post['hoNumber'];
					$this->core->set['ho_date'] = $post['hoDate'] != '' ? $post['hoDate'] : NULL;
					$this->core->set['ho_expired'] = $post['hoExpired'] != '' ? $post['hoExpired'] : NULL;
					$this->core->set['nomor_pbb'] = $post['nomorPBB'];
					$this->core->set['atas_nama'] = $post['atasNama'];
					$this->core->set['jatuh_tempo'] = $post['jatuhTempo'] != '' ? $post['jatuhTempo'] : NULL;
					$this->core->set['nilai_pbb'] = $post['nilaiPBB'];
					$this->core->set['pic_pbb'] = $post['picPBB'];
					$this->core->set['nomor_imb'] = $post['nomorIMB'];
					$this->core->set['date'] = $post['imbDate'] != '' ? $post['imbDate'] : NULL;
					$this->core->set['expired'] = $post['imbExpired'] != '' ? $post['imbExpired'] : NULL;
					$this->core->set['rkt_number'] = $post['rktNumber'];
					$this->core->set['rkt_date'] = $post['rktDate'] != '' ? $post['rktDate'] : NULL;
					$this->core->set['remarks'] = $post['remarks'];
					$this->core->set['updated_date'] = date('Y-m-d H:i:s.u');
					$this->core->set['updated_by'] = $this->session->userdata('username');
					$this->core->set['active'] = 1;
					$this->core->set['status'] = 1;
					
					if ( ! $this->core->view_all('t_mtr_site_l_perijinan', array('site_id' => $id))) {
						$this->core->save('t_mtr_site_l_perijinan');
					} else {
						$this->core->save('t_mtr_site_l_perijinan', array('site_id' => $id));
					}					
					$json = array('error' => FALSE, 'message' => 'Perijinan edited.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'EDIT_TENANCY') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
					
					// save for colloc sell
					$this->db->delete('t_mtr_site_l_tenancy', array('site_id' => $id, 'tenancy_type' => 'COLLOC_SELL'));
					
					$date = date('Y-m-d H:i:s.u');
					$order = 1;
					foreach ($post['tenantCollocSell'] as $kvsell => $vcsell) {
						if ($vcsell != '') {
							$this->core->set['site_id'] = $post['siteId'];
							$this->core->set['available_for_rent'] = $post['availableForRent'];
							$this->core->set['tenancy_type'] = 'COLLOC_SELL';
							$this->core->set['tenant_id'] = $vcsell;
							$this->core->set['tenancy_date'] = $post['tenantDateCollocSell'][$kvsell] != '' ? $post['tenantDateCollocSell'][$kvsell] : NULL;
							$this->core->set['order'] = $order;
							$this->core->set['created_by'] = $this->session->userdata('username');
							$this->core->set['created_on'] = $date;
							$this->core->set['updated_by'] = $this->session->userdata('username');
							$this->core->set['updated_on'] = $date;
							$this->core->set['active'] = 1;
							
							$this->core->save('t_mtr_site_l_tenancy');
						}
						$order++;
					}
					
					// save for colloc buy
					if ($post['seqNumberOfTenant'] > 0) {
						$this->db->delete('t_mtr_site_l_tenancy', array('site_id' => $id, 'tenancy_type' => 'COLLOC_BUY'));
						
						$date = date('Y-m-d H:i:s.u');
						$order = 1;
						foreach ($post['tenantCollocBuy'] as $kvbuy => $vcbuy) {
							if ($vcsell != '') {
								$this->core->set['site_id'] = $post['siteId'];
								$this->core->set['seq_number'] = $post['seqNumberOfTenant'];
								$this->core->set['tenancy_type'] = 'COLLOC_BUY';
								$this->core->set['tenant_id'] = $vcbuy;
								$this->core->set['tenancy_date'] = $post['tenantDateCollocBuy'][$kvbuy] != '' ? $post['tenantDateCollocBuy'][$kvbuy] : NULL;
								$this->core->set['order'] = $order;
								$this->core->set['created_by'] = $this->session->userdata('username');
								$this->core->set['created_on'] = $date;
								$this->core->set['updated_by'] = $this->session->userdata('username');
								$this->core->set['updated_on'] = $date;
								$this->core->set['active'] = 1;
								
								$this->core->save('t_mtr_site_l_tenancy');
							}
							$order++;
						}
					}
					
					$json = array('error' => FALSE, 'message' => 'Tenancy edited.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
			
			if ($this->input->post('cmd') == 'EDIT_AC') {
				
				$json = array();
				$error = array();
				
				$post = $this->core->get_post();
				
				if ($post['brandAC1'] == '') {
					$error[] = 'AC1 Brand can not empty.';
				}
				if ($post['siteId'] == '') {
					$error = array('General Info is not complete.');
				}
				
				if (count($error) == 0) {
				
					$this->core->set['site_id'] = (int) $post['siteId'];
					$this->core->set['ac1_brand'] = (int) $post['brandAC1'];
					$this->core->set['ac1_type'] = $post['typeAC1'];
					$this->core->set['ac1_capacity'] = (int) $post['capacityAC1'];
					$this->core->set['ac1_serial'] = $post['serialNumberAC1'];
					$this->core->set['ac2_brand'] = (int) $post['brandAC2'];
					$this->core->set['ac2_type'] = $post['typeAC2'];
					$this->core->set['ac2_capacity'] = (int) $post['capacityAC2'];
					$this->core->set['ac2_serial'] = $post['serialNumberAC2'];
					$this->core->set['ac3_brand'] = (int) $post['brandAC3'];
					$this->core->set['ac3_type'] = $post['typeAC3'];
					$this->core->set['ac3_capacity'] = (int) $post['capacityAC3'];
					$this->core->set['ac3_serial'] = $post['serialNumberAC3'];
					$this->core->set['remarks'] = trim($post['remarks']);
					
					if ( ! $this->core->view_all('t_mtr_site_l_ac', array('site_id' => $id))) {
						$this->core->save('t_mtr_site_l_ac');
					} else {
						$this->core->save('t_mtr_site_l_ac', array('site_id' => $id));
					}					
					$json = array('error' => FALSE, 'message' => 'AC adited.', 'site_id' => $post['siteId']);
				} else {
					$json = array('error' => TRUE, 'message' => $error, 'site_id' => $post['siteId']);
				}
				
				echo json_encode($json);
				exit;
			}
		}
		
		$this->_site_form($id);
	}
	
	public function delete($id)
	{
		$this->core->update('t_mtr_site_general', array('active' => 0), array('site_id' => $id));
		
		redirect('/site_info');
	}
	
	private function _site_form($id = '')
	{
		$site_type = $this->core->view_all('t_mtr_site_type', NULL, 'site_type_name');
		$data['site_type'] = array('' => '- Choose one -');
		foreach ($site_type as $type) {
			$data['site_type'][$type['type_id']] = $type['site_type_name'];
		}
		
		$site_profile = $this->core->view_all('t_mtr_site_profile', NULL, 'descr');
		$data['site_profile'] = array('' => '- Choose one -');
		foreach ($site_profile as $profile) {
			$data['site_profile'][$profile['profile_id']] = $profile['descr'];
		}
		
		$propinsi = $this->core->view_all('t_mtr_propinsi', NULL, 'descr');
		$data['propinsi'] = array('' => '- Choose one -');
		foreach ($propinsi as $prop) {
			$data['propinsi'][$prop['propinsi_id']] = $prop['descr'];
		}
		
		$data['kabupaten'] = array('' => '- Choose one -');
		$data['kecamatan'] = array('' => '- Choose one -');
		
		$site_specification = $this->site_info_model->get_site_specification();
		$data['site_specification'] = array('' => '');
		foreach ($site_specification as $specification) {
			$data['site_specification'][$specification['specification_id']] = $specification['descr'];
		}
		
		$data['cmd_input_general_info'] = form_hidden('cmd', set_value('cmd', 'ADD_GENERAL_INFO'));
		$data['cmd_input_site_keepers'] = form_hidden('cmd', set_value('cmd', 'ADD_SITE_KEEPERS'));
		$data['cmd_input_pln'] = form_hidden('cmd', set_value('cmd', 'ADD_PLN'));
		$data['cmd_input_tower'] = form_hidden('cmd', set_value('cmd', 'ADD_TOWER'));
		$data['cmd_input_shelter'] = form_hidden('cmd', set_value('cmd', 'ADD_SHELTER'));
		$data['cmd_input_lahan'] = form_hidden('cmd', set_value('cmd', 'ADD_LAHAN'));
		$data['cmd_input_perijinan'] = form_hidden('cmd', set_value('cmd', 'ADD_PERIJINAN'));
		$data['cmd_input_tenancy'] = form_hidden('cmd', set_value('cmd', 'ADD_TENANCY'));
		$data['cmd_input_ac'] = form_hidden('cmd', set_value('cmd', 'ADD_AC'));
		$data['cmd_input_equipment'] = form_hidden('cmd', set_value('cmd', 'ADD_EQUIPMENT'));
		
		if ($this->uri->segment(2) == 'edit') {
			if ($this->core->view_row('t_mtr_site_general', array('site_id' => $id))) {
		
				$data['cmd_input_general_info'] = form_hidden('cmd', set_value('cmd', 'EDIT_GENERAL_INFO'));
				$data['cmd_input_site_keepers'] = form_hidden('cmd', set_value('cmd', 'EDIT_SITE_KEEPERS'));
				$data['cmd_input_pln'] = form_hidden('cmd', set_value('cmd', 'EDIT_PLN'));
				$data['cmd_input_tower'] = form_hidden('cmd', set_value('cmd', 'EDIT_TOWER'));
				$data['cmd_input_shelter'] = form_hidden('cmd', set_value('cmd', 'EDIT_SHELTER'));
				$data['cmd_input_lahan'] = form_hidden('cmd', set_value('cmd', 'EDIT_LAHAN'));
				$data['cmd_input_perijinan'] = form_hidden('cmd', set_value('cmd', 'EDIT_PERIJINAN'));
				$data['cmd_input_tenancy'] = form_hidden('cmd', set_value('cmd', 'EDIT_TENANCY'));
				$data['cmd_input_ac'] = form_hidden('cmd', set_value('cmd', 'EDIT_AC'));
				$data['cmd_input_equipment'] = form_hidden('cmd', set_value('cmd', 'EDIT_EQUIPMENT'));
				
				$general_info = $this->site_info_model->get_general_info_by_id($id);
				$data['general_info'] = $general_info;
				
				$site_keepers = $this->site_info_model->get_site_keepers_by_id($id);
				$data['site_keepers'] = $site_keepers;
				
				$data['ac'] = $this->core->view_row('t_mtr_site_l_ac', array('site_id' => $id));
				$data['perijinan'] = $this->core->view_row('t_mtr_site_l_perijinan', array('site_id' => $id));
				$data['lahan'] = $this->core->view_row('t_mtr_site_l_lahan', array('site_id' => $id));
				$data['shelter'] = $this->core->view_row('t_mtr_site_l_shelter', array('site_id' => $id));
				$data['pln'] = $this->core->view_row('t_mtr_site_l_pln', array('site_id' => $id));
				$data['tower'] = $this->core->view_row('t_mtr_site_l_tower', array('site_id' => $id));
				
				$kabupaten = $this->site_info_model->get_kabupaten($general_info['propinsi']);
				$data['kabupaten'] = array('' => '- Choose one -');
				foreach ($kabupaten as $kab) {
					$data['kabupaten'][$kab['kabupaten_id']] = $kab['descr'];
				}
				
				$kecamatan = $this->site_info_model->get_kecamatan($general_info['kabupaten']);
				$data['kecamatan'] = array('' => '- Choose one -');
				foreach ($kecamatan as $kec) {
					$data['kecamatan'][$kec['kecamatan_id']] = $kec['descr'];
				}
				
				$data['tenancy_sell'] = $this->core->view_all('t_mtr_site_l_tenancy', array('site_id' => $id, 'tenancy_type' => 'COLLOC_SELL'));
				$data['tenancy_buy'] = $this->core->view_all('t_mtr_site_l_tenancy', array('site_id' => $id, 'tenancy_type' => 'COLLOC_BUY'));
		
			} else {
				show_404();
			}
		}
		
		$this->theme->view('form_siteinfo', $data);
	}

	public function tower_provider()
	{
		if (!$this->session->userdata('is_logged_in')) {
			redirect('/login/' . $this->core->encode($this->uri->uri_string()));
		}
		
		$this->load->library('pagination');
		
		$per_page = 20;
		$offset = $this->uri->segment(4);
		$data['tower_provider'] = $this->site_info_model->get_all_tower_provider($per_page, $offset);
		
		$config = array(
			'base_url'		=> site_url('/referensi/wilayah/propinsi'),
			'total_rows'	=> $this->site_info_model->get_rows_tower_provider(),
			'per_page'		=> $per_page,
			'uri_segment'	=> 4
		);
		
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		if ($this->core->is_ajax()) {
			if ($this->input->get('cmd') == 'GET_TOWER_PROVIDER') {
			
				$json = $this->core->view_all('t_mtr_tower_provider', array('active' => 1), 'tower_provider_id');
				
				echo json_encode($json);
				exit;
			}
		}
		
		$this->theme->set('page_title', 'PMTS - Site Info - Tower Provider');		
		$this->theme->view('tower_provider_view', $data);
	}	

	public function site_general()
	{
		if (!$this->session->userdata('is_logged_in')) {
			redirect('/login/' . $this->core->encode($this->uri->uri_string()));
		}
		
		$this->load->library('pagination');
		
		$per_page = 20;
		$offset = $this->uri->segment(4);
		$data['site_general'] = $this->site_info_model->get_all_site($per_page, $offset);
		
		$config = array(
			'base_url'		=> base_url('/referensi/wilayah/propinsi'),
			'total_rows'	=> $this->site_info_model->get_rows_site(),
			'per_page'		=> $per_page,
			'uri_segment'	=> 4
		);
		
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		$this->theme->view('site_general_view', $data);
	}
	
	public function show($id = '')
	{
		if (!$this->session->userdata('is_logged_in')) {
			redirect('/login/' . $this->core->encode($this->uri->uri_string()));
		}
		
		if (empty($id) || !$this->core->view_row('t_mtr_site_general', array('site_id' => $id))) {
			show_404();
		}
		
		$data['general_info'] = $this->core->view_row('t_mtr_site_general', array('site_id' => $id));
		$data['site_keepers'] = $this->core->view_row('t_mtr_site_l_keepers', array('site_id' => $id));
		$data['pln'] = $this->core->view_row('t_mtr_site_l_pln', array('site_id' => $id));
		$data['tower'] = $this->core->view_row('t_mtr_site_l_tower', array('site_id' => $id));
		$data['shelter'] = $this->core->view_row('t_mtr_site_l_shelter', array('site_id' => $id));
		$data['lahan'] = $this->core->view_row('t_mtr_site_l_lahan', array('site_id' => $id));
		$data['perijinan'] = $this->core->view_row('t_mtr_site_l_perijinan', array('site_id' => $id));
		$data['tenancy_sell'] = $this->core->view_all('t_mtr_site_l_tenancy', array('site_id' => $id, 'tenancy_type' => 'COLLOC_SELL'));
		$data['tenancy_buy'] = $this->core->view_all('t_mtr_site_l_tenancy', array('site_id' => $id, 'tenancy_type' => 'COLLOC_BUY'));
		$data['ac'] = $this->core->view_row('t_mtr_site_l_ac', array('site_id' => $id));
		
		$this->theme->view('show_view', $data);
	}

}